﻿namespace LeetCodeProject._0001_1000._001_100._71_80._079;

public class Solution
{
    public bool answer = false;

    public bool Exist(char[][] board, string word)
    {
        int row = board.GetLength(0);
        int col = board[0].GetLength(0);
        bool[,] used = new bool[row, col];
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                if (board[i][j] == word[0])
                {
                    Method(i, j, row, col, board, word, 0, used);
                }
            }
        }

        return answer;
    }

    public void Method(int i, int j, int row, int col, char[][] board, string word, int index, bool[,] used)
    {
        if (answer == true) //已经有满足条件的了  不再递归 必须要加这个不然会超时
        {
            return;
        }

        if (index == word.Length)
        {
            answer = true;
            return;
        }

        if (i < 0 || i > row - 1 || j < 0 || j > col - 1 || used[i, j] == true || board[i][j] != word[index])
        {
            return;
        }

        //做出选择
        used[i, j] = true;
        //递归
        Method(i - 1, j, row, col, board, word, index + 1, used);
        Method(i + 1, j, row, col, board, word, index + 1, used);
        Method(i, j - 1, row, col, board, word, index + 1, used);
        Method(i, j + 1, row, col, board, word, index + 1, used);
        //取消选择
        used[i, j] = false;
    }
}